<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader
    @on-add="Add"
    :todos="list"
    @choice-all="choiceAll"
    ></TodoHeader>
    <TodoMain :todos="list" @on-del="onDel"></TodoMain>
    <TodoFooter @del-all="delAll"></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import './styles/base.css'
import './styles/index.css'
import axios from 'axios'

import TodoHeader from './components/TodoHeader'
import TodoMain from './components/TodoMain'
import TodoFooter from './components/TodoFooter'
axios.defaults.baseURL = 'http://liufusong.top:3001'

export default {
  data () {
    return {
      list: []
    }
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  methods: {
    async choiceAll (bol) {
      try {
        const res = await axios({
          method: 'GET',
          url: '/all/' + (bol ? 1 : 0)
        })
        console.log(res)
        this.getData()
      } catch (error) {
        console.log(error)
      }
    },
    async delAll () {
      try {
        const res = await axios({
          method: 'delete',
          url: '/deleteall'
        })
        console.log(res)
        this.getData()
      } catch (error) {
        console.log(error)
      }
    },
    async onDel (val) {
      try {
        const res = await axios({
          method: 'delete',
          url: '/list/' + val
        })
        console.log(res)
        this.getData()
      } catch (error) {
        console.log(error)
      }
    },
    async Add (val) {
      try {
        const res = await axios({
          method: 'POST',
          url: '/add',
          data: {
            title: val
          }
        })
        console.log(res)
        this.getData()
      } catch (error) {
        console.log(error)
      }
    },
    async getData () {
      try {
        const res = await axios({
          url: '/list'
        })
        this.list = res.data
        console.log(this.list)
      } catch (error) {
        console.log(error)
      }
    }
  },
  created () {
    this.getData()
  }
}
</script>
